//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    custSourceListData: app.globalData.custSourceListData,
    custStatuListData: app.globalData.custStatuListData,
    yearListData: app.globalData.yearListData,
    mileageListData: app.globalData.mileageListData,
    colorListData: app.globalData.colorListData,
    cityListData: app.globalData.cityListData,
    storeListData: app.globalData.storeListData,
    carNoRegionList: app.globalData.carNoRegionList,
    request_common_para:app.globalData.request_common_para,
    index: 0,
    chosen: '',
    id: '',
    cust_name: '',
    cust_tel: '',
    car_model: "+",
    carType:0,
    begin_price: '',
    end_price: '',
    note: '',
    other_car_no_region: "",
    messageList: [],
    updateDateFormat: '',
    isDisabled: true,
    isYearShow: false,
    initYearVal: "不限",
    isMileageShow: false,
    initMileageVal: "不限",
    isCarColorShow: false,
    initCarColorVal: "其他",
    isCarSales:false,
    vinCode:''

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.intiSysConfigData()
    this.setData({
      custSourceListData: app.globalData.custSourceListData,
      storeListData: app.globalData.storeListData
    })
    wx.request({
      url: app.getHost()+"/datalake/v1/userManages/" + options.id,
      method: "GET",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: res => {
        if (res.statusCode == 200) {
          let custSourceListData = this.data.custSourceListData;
          for (var i in custSourceListData) {
            custSourceListData[i].checked = false
            if (custSourceListData[i].name == res.data.customerSource) {
              custSourceListData[i].checked = true
            }
          }
          let custStatuListData = this.data.custStatuListData
          for (var i in custStatuListData) {
            custStatuListData[i].checked = false
            if (custStatuListData[i].name == res.data.state_name) {
              custStatuListData[i].checked = true
            }
          }
          let colorListData = this.data.colorListData
          let requirementColors = res.data.requirementColor.split("|")
          for (var i in colorListData) {
            colorListData[i].checked = false
            for (var k in requirementColors) {
              if (colorListData[i].name == requirementColors[k]) {
                colorListData[i].checked = true
              }
            }
          }
          let requirementYears = res.data.requirementYear.split("|")
          let yearListData = this.data.yearListData
          for (var i in yearListData) {
            yearListData[i].checked = false
            for (var k in requirementYears) {
              if (yearListData[i].name == requirementYears[k]) {
                yearListData[i].checked = true
              }
            }
          }
          let mileageListData = this.data.mileageListData
          let requirementMileages = res.data.requirementMileage.split("|")
          for (var i in mileageListData) {
            mileageListData[i].checked = false
            for (var k in requirementMileages) {
              if (mileageListData[i].name == requirementMileages[k]) {
                mileageListData[i].checked = true
              }
            }
          }
          var requirementSettled_in = res.data.requirementSettled_in
          var other_car_no_region = ""
          let carNoRegionList = this.data.carNoRegionList
          for (let i in carNoRegionList) {
            if (requirementSettled_in == carNoRegionList[i].value) {
              carNoRegionList[i].checked = true
            } else if (requirementSettled_in != "沪A" && requirementSettled_in != "沪C" && requirementSettled_in!="") {
              carNoRegionList[i].checked = true
              other_car_no_region = requirementSettled_in
            }
          }
          let store_name = res.data.store_name
          let storeListData = app.globalData.storeListData
          for (var i in storeListData) {
            if (storeListData[i] == store_name) {
              this.setData({ index: i })
              break;
            }
          }
          let vinCode = res.data.vin_code;
          if (vinCode) {
            this.setData({
              isCarSales: true,
              vinCode: vinCode
            })
          }
          let carTypeCount = res.data.carModel.split(",").length
          let carModels = res.data.carModel
          let updateDateFormat = res.data.updateDateFormat
          let req_data =this.data.request_common_para;
          req_data.customerPhone = res.data.customerPhone
          req_data.carModel = res.data.carModel.split(",")
        //  console.info("https://mcc.mtmche.cn/datalake/v1/userManages/messages request params:" + JSON.stringify(req_data))
          wx.request({
            url: app.getHost()+'/datalake/v1/userManages/messages',
            data: req_data,
            method: "POST",
            success: res => {
             


              let messageList = []
              for (let i in res.data.mileage_list) {
                let message = {}
                let obj = res.data.list[i]
                let update_time = obj.update_time
                let user_type = obj.user_type
                let update_user = obj.update_user
                let content = obj.content
                message.update_time = update_time
                message.user_type = user_type
                message.update_user = update_user
                message.content = content
                if (user_type.length > 0 && user_type != "url" && user_type != "null") {
                  let k = content.indexOf("到店") + 2
                  let pageId = content.substr(k)
                  let content_str = content.substring(0, k)
                  message.user_type = app.getHost() +"/datalake/v1/direct/error/" + pageId
                  message.content = content_str
                }
                messageList.push(message)
              }


              this.setData({
                messageList: messageList
              })

            },
            fail: function (res) {

            },
            complete: function (res) {

            }
          })

          this.setData({
            id: res.data.id,
            cust_name: res.data.customerName,
            cust_tel: res.data.customerPhone,
            custSourceListData: custSourceListData,
            custStatuListData: custStatuListData,
            colorListData: colorListData,
            yearListData: yearListData,
            mileageListData: mileageListData,
            car_model: carModels,
            begin_price: res.data.budgetLimitMin,
            end_price: res.data.budgetLimitMax,
            carNoRegionList: carNoRegionList,
            note: res.data.note,
            other_car_no_region: other_car_no_region,
            updateDateFormat: updateDateFormat,
            carType:carTypeCount,
            initYearVal: res.data.requirementYear,
            initMileageVal: res.data.requirementMileage,
            initCarColorVal: res.data.requirementColor

          })
        }
      },
      complete: function (res) { }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 表单提交事件
   */
  submitCustInfo: function (e) {
    var data = this.data.request_common_para;
    let form_id = e.detail.formId
    var city = this.data.cityListData[this.data.index]
    var cust = e.detail.value
    var cust_name = cust.cust_name //客户姓名
    if (!cust_name) {
      app.noTitlemodalTap("请输入客户姓名")
      return;
    }
    var cust_tel = cust.cust_tel //客户电话
    if (!cust_tel) {
      app.noTitlemodalTap("请输入客户手机")
      return;
    }
    var car_source = cust.car_source //客户来源
    if (!car_source) {
      app.noTitlemodalTap("请选择客户来源")
      return;
    }

    var cust_status = cust.cust_status //购车意向
    if (!cust_status) {
      app.noTitlemodalTap("请选择意向等级")
      return;
    }
    var begin_price = cust.begin_price //价格
    var end_price = cust.end_price
    if (!begin_price || !end_price) {
      app.noTitlemodalTap("请输入预购车算")
      return;
    }

    var car_model = cust.car_model //车型
    if (!car_model) {
      app.noTitlemodalTap("请输入车型")
      return;
    }
    var car_year_val = this.data.initYearVal
    /**
    var car_year_list = cust.year_list //年限
    for (var i in car_year_list) {
      car_year_val += car_year_list[i]
      if (i != car_year_list.length - 1) {
        car_year_val += "|"
      }
    }
    if (!car_year_val) {
      app.noTitlemodalTap("请选择年限要求")
      return;
    }
     */
    var car_mileage_val = this.data.initMileageVal
    /**
    var car_mileage_list = cust.mileage_list //里程
    for (var i in car_mileage_list) {
      car_mileage_val += car_mileage_list[i];
      if (i != car_mileage_list.length - 1) {
        car_mileage_val += "|"
      }
    }
    if (!car_mileage_val) {
      app.noTitlemodalTap("请选择里程要求")
      return;
    }
    */
    var car_color_val = this.data.initCarColorVal
    /**
    var car_color_list = cust.car_color //颜色
    for (var i in car_color_list) {
      car_color_val += car_color_list[i]
      if (i != car_color_list.length - 1) {
        car_color_val += "|"
      }
    }
    if (!car_color_val) {
      app.noTitlemodalTap("请选择颜色要求")
      return;
    }
    */
    var car_no_region = cust.car_no_region //落地户
    var remark = cust.remark //备注
    var jianyi = cust.jianyi //建议
    var other_car_no_region = cust.other_car_no_region //外地车牌
    /**
    if (!car_mileage_val) {
      app.noTitlemodalTap("请选择落地户")
      return;
    }
     */
    app.toast3Tap()

    data.id = cust.id
    data.audit = 1
    data.customerPhone = cust_tel
    data.audit_user = data.customer_name
    data.form_id = form_id
    /**data.customerSource = car_source
    data.note = jianyi
    data.requirementColor = car_color_val
    data.requirementMileage = car_mileage_val
    data.requirementSettled_in = car_no_region
    data.requirementYear = car_year_val
    data.state = cust_status
    data.sumParam = 0
    data.budgetLimitMax = end_price
    data.budgetLimitMin = begin_price
    data.carModel = car_model
    data.city = city
    data.content = remark
    data.count = 0
    data.customerName = cust_name
    */
    console.info(data)
    wx.request({
      url: app.getHost()+ '/datalake/v1/userManages/' + data.id,
      data: data,
      method: "PUT",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.statusCode == 200) {
          app.toast1Tap()
          console.info("创建客户：" + JSON.stringify(res.data))
          wx.navigateTo({
            url: 'custInfoAuditing',
          })
        }
      },
      complete: function (res) { },
      fail: function (res) {
        console.error(res);
      }
    })
  },
  bindinputCarModel: function (e) {
    wx.navigateTo({
      url: 'carModelType?selectedCarModelType=' + this.data.car_model + '&carType=' + this.data.carType
    })
  },
  userTypeUrl: function (e) {
    var id = e.currentTarget.id
    console.info(id)
    if (id != '' && id!=''){
      wx.navigateTo({
        url: '../out/out?html_src=' + id,
      })
    }
    
  },
  bindtapisYearShow: function (e) {
    if (this.data.isYearShow) {
      this.setData({ isYearShow: false })
    } else {
      this.setData({ isYearShow: true })
    }
  },
  bindchangeYear: function (e) {
    let year_check_list = e.detail.value;
    let val = "";
    for (let i in year_check_list) {
      val += year_check_list[i]
      if (i != year_check_list.length - 1) {
        val += "|"
      }
    }
    if (val == "") {
      val = "不限"
    }
    this.setData({ initYearVal: val })
  },
  bindtapisMileageShow: function (e) {
    if (this.data.isMileageShow) {
      this.setData({ isMileageShow: false })
    } else {
      this.setData({ isMileageShow: true })
    }
  },
  bindchangeMileage: function (e) {
    let mileage_list = e.detail.value;
    let val = "";
    for (let i in mileage_list) {
      val += mileage_list[i]
      if (i != mileage_list.length - 1) {
        val += "|"
      }
    }
    if (val == "") {
      val = "不限"
    }
    this.setData({ initMileageVal: val })
  },
  bindtapisCarColorShow: function (e) {
    if (this.data.isCarColorShow) {
      this.setData({ isCarColorShow: false })
    } else {
      this.setData({ isCarColorShow: true })
    }
  },
  bindchangeCarColor: function (e) {
    let car_color_list = e.detail.value;
    let val = "";
    for (let i in car_color_list) {
      val += car_color_list[i]
      if (i != car_color_list.length - 1) {
        val += "|"
      }
    }
    if (val == "") {
      val = "不限"
    }
    this.setData({ initCarColorVal: val })
  },
  bindtapCallTel:function(e){
    if (this.data.cust_tel){
      wx.makePhoneCall({
        phoneNumber: this.data.cust_tel,
        success:res=>{
          
        }
      })
    }
  }
})